package com.example.service;

import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;
    public Map<String,Object> createAccount(User user){
        int result = userMapper.insertUser(user);
        Map<String,Object> resultMap = new HashMap<>();
        if (result > 0){
            System.out.println("注册成功");
            resultMap.put("code",200);
            resultMap.put("message",user.getUsername()+"注册成功");
        }else {
            resultMap.put("code",400);
            resultMap.put("message","注册失败");
        }
        return resultMap;
    }

    public Map<String,Object> loginAccount(User user){
        Map<String,Object> resultMap = new HashMap<>();
        //根据用户名查询用户
        List<User> userList = userMapper.selectUserByName(user.getUsername());
        //查询不到结果，返回：给用户不存在
        if (userList == null || userList.isEmpty()){
            resultMap.put("code",400);
            resultMap.put("message","用户不存在");
            return resultMap;
        }
        User u = userList.get(0);
        if(!u.getPassword().equals(u.getPassword())){
            resultMap.put("code",400);
            resultMap.put("message","用户名或密码错误");
        }
        resultMap.put("code",200);
        resultMap.put("message",u.getUsername()+"登陆成功");

        return resultMap;
    }
}
